package leetcode;

public class _2765 {
    /**
     * 最长交替子序列
     */
    class Solution {
        public int alternatingSubarray(int[] nums) {
            int n = nums.length;
            int length = 0;
            int ans = 0;
            for(int i=1;i<n;i++){
                if(length>0&&nums[i]==nums[i-2]){
                    length++;
                }else if(nums[i]-nums[i-1]==1){
                    length=2;
                }else{
                    length=0;
                }
                ans = Math.max(ans,length);
            }
            return ans;
        }
    }
}
